package cn.ghostman.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import cn.ghostman.app.R;

public class LaunchActivity extends Activity {

    private static final String TAG = LaunchActivity.class.getSimpleName();

    /**
     * Activity销毁时remove Handler
     */
    private Handler handler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        // 2秒钟
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startMainActivity();

                Log.e(TAG, Thread.currentThread().getName());
            }
        }, 2000);
    }

    /**
     * 手指滑动事件
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        startMainActivity();
        return super.onTouchEvent(event);
    }

    /**
     * 销毁事件
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 移除handler
        if(handler != null)
            handler.removeCallbacksAndMessages(null);
    }

    private void startMainActivity() {
        Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
        startActivity(intent);

        finish();
    }
}
